package com.yjf.ljr.activity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.AcountManageDataClass;
import com.yjf.ljr.dataclass.AcountManageDataClass.AcountManageDataInfo;
import com.yjf.ljr.dataclass.CheckInvestPasswordClass;
import com.yjf.ljr.dataclass.InvestDetailDataClass.ProjectInfo;
import com.yjf.ljr.dataclass.InvestDetailProjectClass;
import com.yjf.ljr.dataclass.InvestDetailProjectClass.ProjectItemInfo;
import com.yjf.ljr.dataclass.MyInvestDataClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.PasswordDialog;

public class ProjectDetailActivity extends IjActivity implements OnClickListener {

	// 初始化控件
	@ID("tvProjectTitle")
	private TextView mTvProjectTitle; //
	@ID("tvProjectProgress")
	private TextView mTvProjectProgress; //
	@ID("tvProjectBorrow")
	private TextView mTvProjectBorrow; //
	@ID("tvProjectIncome")
	private TextView mTvProjectIncome; //
	@ID("tvProjectDate")
	private TextView mTvProjectDate; //
	@ID("tvProjectPayMethod")
	private TextView mTvProjectPayMethod; //
	@ID("pbProjectDetail")
	private ProgressBar mPbProjectDetail; // 进度条
	@ID("btnProjectInvest")
	private Button mBtnProjectInvest; //
	@ID("llBaseRate")
	private LinearLayout mLlBaseRate; //
	@ID("llProjectInvestDetail")
	private LinearLayout mLlProjectInvestDetail; //
	@ID("llBlueprint")
	private LinearLayout mLlBlueprint; //
	@ID("llProjectIinformation")
	private LinearLayout mLlProjectIinformation; // 项目信息
	
	@ID("llTeamInformation")
	private LinearLayout mLlTeamInformation;

	private String demandId;// 项目Id
	private String idemandId;// 投资明细传的项目Id
	private String tradeId;// 查看投资明细的ID
	private String interestRate;// 年利化率
	private String timeLimit;// 借款期限
	private String currenttime;
	public static boolean AppInvestDialgFlag = false;// 投资成功返回标志物用于刷新
	public static String AppInvestDialgProgress = "";// 投资成功的进度

	private String loanNote;// 项目信息
	private String loanStatement;// 担保机构简介
	private String guaranteeStatement;// 风控措施
	private String info3;//
	private String sponsorStatement;// 担保机构意见

	// 其它变量
	private ProjectInfo mProjectInfo;
	private ProjectItemInfo mProjectItemInfor;

	static ProjectDetailActivity instace;

	private String flag_success = "1";
	private String message = "";
	private DecimalFormat format;

	private final int FLAG_INVEST_LOGIN = 1;
	private boolean isDirectInvest = true;
	private String typeID;
	private String projectInfo;// 涉诉信息

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
				.detectNetwork() // or .detectAll() for all detectable problems
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog()
				.penaltyDeath().build());
		setContentView(R.layout.activity_project_detail);

		initControls();
		idemandId = getIntent().getStringExtra("demandId");
		typeID = getIntent().getStringExtra("typeID");
		if (TextUtils.isEmpty(idemandId)) {
			getIntentParams();
		}
		setDataFromIntent(mProjectInfo);
		showProgressDialog();
		new ProjectDetailTask().execute();
	}

	/**
	 * 初始化控件
	 * 
	 * @throws ParseException
	 **/
	private void initControls() {
		setLeftBtnClick();
		setTitleStr("项目详情");
		format = new DecimalFormat("#.00");
		mLlBaseRate.setOnClickListener(this);
		mLlProjectInvestDetail.setOnClickListener(this);
		mLlBlueprint.setOnClickListener(this);
		mBtnProjectInvest.setOnClickListener(this);
		mLlProjectIinformation.setOnClickListener(this);
		mLlTeamInformation.setOnClickListener(this);

		Intent intent = getIntent();

		ShapeDrawable drawable = new ShapeDrawable(new RoundRectShape(new float[] { 4, 4, 4, 4, 4, 4, 4, 4 }, null,
				null));
		if ("2".equals(intent.getStringExtra("Statusdetail"))) {
			mBtnProjectInvest.setText("投资已满");
			mBtnProjectInvest.setClickable(false);
			drawable.getPaint().setColor(Color.parseColor("#a0d861"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);
			return;
		}
		if ("3".equals(intent.getStringExtra("Statusdetail"))) {

			mBtnProjectInvest.setText("未到时间");
			mBtnProjectInvest.setClickable(false);

			drawable.getPaint().setColor(Color.parseColor("#b4b4b4"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);

			return;
		}
		if ("4".equals(intent.getStringExtra("Statusdetail"))) {
			mBtnProjectInvest.setText("还款完成");
			mBtnProjectInvest.setClickable(false);

			drawable.getPaint().setColor(Color.parseColor("#00819e"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);
			return;
		}
		if ("6".equals(intent.getStringExtra("Statusdetail"))) {
			mBtnProjectInvest.setText("已过投资时间");
			mBtnProjectInvest.setClickable(false);

			drawable.getPaint().setColor(Color.parseColor("#00839F"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);

			return;
		}
		if ("8".equals(intent.getStringExtra("Statusdetail"))) {
			mBtnProjectInvest.setText("交易失败");
			mBtnProjectInvest.setClickable(false);

			drawable.getPaint().setColor(Color.parseColor("#808080"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);

			return;
		}
		if ("7".equals(intent.getStringExtra("Statusdetail")) || "5".equals(intent.getStringExtra("Statusdetail"))) {
			mBtnProjectInvest.setText("正在还款");
			mBtnProjectInvest.setClickable(false);

			drawable.getPaint().setColor(Color.parseColor("#07b9c3"));
			mBtnProjectInvest.setBackgroundDrawable(drawable);
			return;
		}
		/*
		 * String startTime = SPreferences.getData(mContext, "",
		 * "startTimeInvestDetail").toString(); startTime =
		 * startTime.replace(" ", ""); startTime = startTime.replace("-", "");
		 * startTime = startTime.replace(":", "");
		 * 
		 * long bb = changeMillisecond(startTime); long bba = getSystemtime();
		 * 
		 * if (getSystemtime() < changeMillisecond(startTime)) {
		 * mBtnProjectInvest.setClickable(false);
		 * mBtnProjectInvest.setBackgroundColor(Color.parseColor("#b4b4b4"));
		 * mBtnProjectInvest.setText("暂未开始"); }
		 */

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (AppInvestDialgFlag) {
			AppInvestDialgFlag = false;
			/** 进度条 **/
			if (!TextUtils.isEmpty(AppInvestDialgProgress)) {
				double progress = 0;
				try {
					String strProgress = AppInvestDialgProgress;
					if (strProgress.endsWith("%")) {
						strProgress = strProgress.substring(0, strProgress.length() - 1);
					}
					progress = Double.parseDouble(strProgress);
				} catch (Exception e) {
					e.printStackTrace();
				}
				String temp_progress = "";
				if (!TextUtils.isEmpty(progress + "")) {
					temp_progress = format.format(progress);
				} else {
					temp_progress = "0.00";
				}
				mPbProjectDetail.setProgress((int) (progress));
				AppUtil.setViewText(mTvProjectProgress, temp_progress + "%");
				AppInvestDialgProgress = "";
			}
		}
	}

	private long getSystemtime() {

		URL urlTime = null;
		URLConnection uc = null;
		try {
			urlTime = new URL("http://www.bjtime.cn");
			uc = urlTime.openConnection();
			uc.connect();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 发出连接
		long ld = uc.getDate(); // 取得网站日期时间
		Date date = new Date(ld); // 转换为标准时间对象

		SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMddHHmmss");// 24小时制,12小时制则HH为hh
		String UserTime = sdformat.format(date);

		// 分别取得时间中的小时，分钟和秒，并输出
		return changeMillisecond(UserTime);

	}

	private long changeMillisecond(String dateTime) {

		Calendar c = Calendar.getInstance();

		try {
			c.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(dateTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Long.parseLong(c.getTimeInMillis() + "");
	}

	private long getTimeNow() {
		// ***** 得到系统时间 ***/

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int sec = c.get(Calendar.SECOND);
		String tempMonth;
		String tempDay;
		String temphour;
		String tempminute;
		String tempsec;

		if ((month + "").length() < 2) {
			tempMonth = "0" + month;
		} else {
			tempMonth = month + "";
		}

		if ((day + "").length() < 2) {
			tempDay = "0" + day;
		} else {
			tempDay = day + "";
		}
		if ((day + "").length() < 2) {
			temphour = "0" + hour;
		} else {
			temphour = hour + "";
		}
		if ((day + "").length() < 2) {
			tempminute = "0" + minute;
		} else {
			tempminute = minute + "";
		}
		if ((day + "").length() < 2) {
			tempsec = "0" + sec;
		} else {
			tempsec = sec + "";
		}

		return changeMillisecond(year + tempMonth + tempDay + temphour + tempminute + tempsec + ""); // return
																										// 0;
	}

	private void getIntentParams() {
		Intent i = getIntent();
		Bundle b = i.getBundleExtra("bundle");
		mProjectInfo = (ProjectInfo) b.getSerializable("item");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case CommonData.REQUEST_CODE_LOGIN:
				if (intent != null && intent.getBooleanExtra(LockPatternActivity.FROM_LOCKPATTERN, false)) {
					break;
				}
				showProgressDialog();

				if (mProjectInfo == null) {
					new MyInvestActivityTask(idemandId).execute();
				} else {
					new MyInvestActivityTask(mProjectInfo.demandId).execute();
				}
				break;
			case CommonData.REQUEST_CODE_PROJECT:
//				if (!TextUtils.isEmpty(intent.getStringExtra("investProgress"))) {
//
//					mPbProjectDetail.setProgress((int) Double.parseDouble((intent.getStringExtra("investProgress"))));
//					// AppUtil.setViewText(mTvProjectProgress,
//					// intent.getStringExtra("investProgress") + "%");
//					new ProjectDetailTask().execute();
//				}
				new ProjectDetailTask().execute();
				break;
			case FLAG_INVEST_LOGIN:
				getInvestPromise(isDirectInvest);
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	private void setDataFromIntent(ProjectInfo info) {
		if (info == null) {
			return;
		}

		/** 进度条 **/
		if (!TextUtils.isEmpty(info.investProgress)) {
			double progress = 0;
			try {
				String strProgress = info.investProgress;
				if (strProgress.endsWith("%")) {
					strProgress = strProgress.substring(0, strProgress.length() - 1);
				}
				progress = Double.parseDouble(strProgress);
			} catch (Exception e) {
				e.printStackTrace();
			}
			String temp_progress = "";
			if (!TextUtils.isEmpty(progress + "")) {
				temp_progress = format.format(progress);
			} else {
				temp_progress = "0.00";
			}
			mPbProjectDetail.setProgress((int) (progress));
			AppUtil.setViewText(mTvProjectProgress, temp_progress + "%");
		}

		// //////////////////////

		String tempBorrowMoney = info.loanAmount;
		/** 借款金额 **/
		if (!TextUtils.isEmpty(tempBorrowMoney)) {
			double borrowMoney = 0;
			try {
				borrowMoney = Double.parseDouble(tempBorrowMoney.replace(",", ""));
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (borrowMoney < 10000) {
				mTvProjectBorrow.setText(borrowMoney + "元");
			} else {
				int tenThousand = (int) borrowMoney / 10000;
				double thousand = (borrowMoney % 10000) / 10000d;
				if (thousand > 0) {
					mTvProjectBorrow.setText((tenThousand + thousand) + "万");
				} else {
					mTvProjectBorrow.setText(tenThousand + "万");
				}
			}

		}

		// //////////////////

		// String temp_borrowMoney = info.loanAmount;
		// temp_borrowMoney = temp_borrowMoney.replace(",", "");
		// String temp_aa;
		// if (!("-1".equals(temp_borrowMoney.indexOf(".") + ""))) {
		// temp_aa = temp_borrowMoney.substring(0,
		// temp_borrowMoney.indexOf("."));
		// } else {
		// temp_aa = temp_borrowMoney;
		// }
		// int temp_a = (Integer.parseInt(temp_aa)) / 10000;
		//
		// int temp_b = (Integer.parseInt(temp_aa)) % 10000;
		// int temp_c = temp_b;
		// int temp_d = 0;
		// String temp = "";
		// if (!("0".equals(temp_b + ""))) {
		// /*
		// * for (int i = 0; i < (temp_b + "").length(); i++) { int aaaaa =
		// * (int) Math.pow(10, (temp_b + "").length() - i - 1); temp_d =
		// * temp_c / aaaaa; temp_c = temp_c % aaaaa; temp = temp + temp_d; }
		// */
		// temp = temp_aa.substring((temp_aa).indexOf(temp_a + "") + (temp_a +
		// "").length(), temp_aa.length());
		// if (TextUtils.isEmpty(temp)) {
		// AppUtil.setViewText(mTvProjectBorrow, "￥" + temp_a + "万");
		// } else {
		// String temp_Suffix = "";
		// while ("0".equals(temp.substring(0, 1))) {
		// temp_Suffix += "0";
		// temp = temp.substring(1, temp.length());
		// }
		// int temp_next = Integer.parseInt(temp);
		// while ("0".equals((temp_next % 10) + "")) {
		// temp_next = temp_next / 10;
		//
		// }
		//
		// AppUtil.setViewText(mTvProjectBorrow, "￥" + temp_a + "." +
		// temp_Suffix + temp_next + "万");
		//
		// }
		// } else {
		// AppUtil.setViewText(mTvProjectBorrow, "￥" + temp_a + "万");
		// }

		AppUtil.setViewText(mTvProjectIncome, info.interestRate);
		AppUtil.setViewText(mTvProjectDate, info.timeLimit);
	}

	/*********** 调用此接口是为了判断用户有没有登录. ************/
	class MyInvestActivityTask extends AsyncTask<Void, Void, Void> {
		private String projectId;// 项目ID
		private String mFailLog;
		private MyInvestDataClass date = new MyInvestDataClass();

		public MyInvestActivityTask(String projectId) {
			this.projectId = projectId;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			RequestObject requestObjectSms = new RequestObject();
			requestObjectSms.method = "gotoInvest";
			requestObjectSms.map.put("demandId", projectId);

			String requestSmsUrl = RequestBuilder.build(requestObjectSms);
			try {
				IjConnect.doRequest(requestSmsUrl, date);
				flag_success = date.code;
				message = date.msg;
				if (date.code == null) {
					mFailLog = getResources().getString(R.string.loaddata_exception);
				} else if (!date.code.equals(CommonData.RESULT_SUCCESS)) {
					mFailLog = date.msg;
				}
			} catch (Exception e) {
				// TODO: handle exception
				mFailLog = getResources().getString(R.string.loaddata_exception);
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if ("-1".equals(date.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(mFailLog)) { // showToast(mFailLog);
				showToast(mFailLog);
			} else {
				final PasswordDialog.Builder mBuilder = new PasswordDialog.Builder(mContext);
				mBuilder.setTitle("融资密码");
				mBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						if (TextUtils.isEmpty(mBuilder.getPassWord())) {
							showToast("请输入融资密码！！！");
						} else {
							new CheckInvestPasswordTask(demandId, mBuilder.getPassWord()).execute();
							dialog.dismiss();
						}
					}
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				});
				mBuilder.create().show();
			}
			dismissProgressDialog();
		}

	}

	/**********************/

	/** 获取captcha校验码 验证用户是否在线 ， 否就跳转到登陆界面 **/

	/*
	 * class CheckCodeTask extends AsyncTask<Void, Void, Void> {
	 * 
	 * private String mFailLog; private GetCheckCodeClass data = new
	 * GetCheckCodeClass();
	 * 
	 * @Override protected void onPreExecute() {
	 * 
	 * super.onPreExecute(); }
	 * 
	 * @Override protected Void doInBackground(Void... params) { RequestObject
	 * requestObject = new RequestObject(); requestObject.method =
	 * "getCheckCode"; String requestUrl = RequestBuilder.build(requestObject);
	 * 
	 * try { IjConnect.doRequest(requestUrl, data);
	 * 
	 * if (data.code != null) { mFailLog =
	 * getResources().getString(R.string.loaddata_exception); } } catch
	 * (Exception e) { mFailLog =
	 * getResources().getString(R.string.loaddata_exception);
	 * e.printStackTrace(); } return null; }
	 * 
	 * @Override protected void onPostExecute(Void result) { if
	 * ("-1".equals(data.code)) { loginWebview(); return; } if
	 * (!TextUtils.isEmpty(mFailLog)) { // showToast(mFailLog);
	 * showToast(mFailLog); } else { final PasswordDialog.Builder mBuilder = new
	 * PasswordDialog.Builder(mContext); mBuilder.setTitle("融资密码");
	 * mBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
	 * 
	 * @Override public void onClick(DialogInterface dialog, int arg1) { if
	 * (TextUtils.isEmpty(mBuilder.getPassWord())) { showToast("请输入融资密码！！！"); }
	 * else { new CheckInvestPasswordTask(SPreferences.getData(mContext, "",
	 * "projectId").toString(), mBuilder.getPassWord()).execute();
	 * dialog.dismiss(); } } }).setNegativeButton("取消", new
	 * DialogInterface.OnClickListener() {
	 * 
	 * @Override public void onClick(DialogInterface dialog, int arg1) {
	 * dialog.dismiss(); } }); mBuilder.create().show(); }
	 * dismissProgressDialog(); } }
	 */

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnProjectInvest: // 我要投资

			if (mProjectItemInfor != null && !TextUtils.isEmpty(mProjectItemInfor.tradeId)) {
				if ("1".equals(mProjectItemInfor.isDirectional.toString())) {
					showProgressDialog();
					new MyInvestActivityTask(demandId).execute();// 调用此接口是为了判断用户有没有登录.
				} else {

					// Intent i = new Intent(mContext, MyInvestActivity.class);
					// if (mProjectInfo == null) {
					// i.putExtra("demandId", demandId);
					// i.putExtra("timeLimit", timeLimit);
					// i.putExtra("tradeId", tradeId);
					//
					// } else {
					// i.putExtra("demandId", mProjectInfo.demandId);
					// i.putExtra("timeLimit", mProjectInfo.timeLimit);
					// i.putExtra("tradeId", mProjectInfo.tradeId);
					// }
					// i.putExtra("interestRate", interestRate);
					// i.putExtra("tradeId", mProjectItemInfor.tradeId);
					// startActivityForResult(i,
					// CommonData.REQUEST_CODE_PROJECT);

					getInvestPromise(true);
				}
			}

			break;
		case R.id.llBaseRate:
			Intent intent = new Intent(this, ProjectRelationInfoActivity.class);
			intent.putExtra("demandId", demandId);
			intent.putExtra("type", ProjectRelationInfoActivity.BASE_RATE);
			startActivity(intent);
			break;
		case R.id.llProjectInvestDetail: // 投资明细
			if (mProjectItemInfor != null && !TextUtils.isEmpty(mProjectItemInfor.tradeId)) {
				Intent i = new Intent(mContext, InvestDetailActivity.class);
				i.putExtra("tradeId", mProjectItemInfor.tradeId);
				startActivity(i);
			}
			break;
		case R.id.llBlueprint:
			Intent bluePrintIntent = new Intent(this, ProjectRelationInfoActivity.class);
			bluePrintIntent.putExtra("demandId", demandId);
			bluePrintIntent.putExtra("type", ProjectRelationInfoActivity.BLUEPRINT);
			startActivity(bluePrintIntent);
			break;
		case R.id.llProjectIinformation:// 项目信息
			Intent projectIntent = new Intent(this, ProjectRelationInfoActivity.class);
			projectIntent.putExtra("demandId", demandId);
			projectIntent.putExtra("type", ProjectRelationInfoActivity.PROJECT_INFO);
			startActivity(projectIntent);
			break;
		case R.id.llTeamInformation:
			Intent teamIntent = new Intent(this, ProjectRelationInfoActivity.class);
			teamIntent.putExtra("demandId", demandId);
			teamIntent.putExtra("type", ProjectRelationInfoActivity.TEAM_INTO);
			startActivity(teamIntent);
			break;
		}
	}

	class ProjectDetailTask extends AsyncTask<Void, Void, String> {

		private InvestDetailProjectClass dc = new InvestDetailProjectClass();

		@Override
		protected String doInBackground(Void... params) {
			RequestObject reqObj = new RequestObject();
			reqObj.method = "appProjectDetail";
			if ("tradeId".equals(typeID)) {
				reqObj.map.put("tradeId", idemandId);
			} else if (mProjectInfo == null) {
				reqObj.map.put("demandId", idemandId);
			} else {
				reqObj.map.put("demandId", mProjectInfo.demandId);
			}
			return requestDataAndGetErrorMsg(reqObj, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
				mBtnProjectInvest.setText("立即支持");
				mBtnProjectInvest.setBackgroundColor(Color.parseColor("#03b888"));
			} else {
				if (dc.projectList != null && dc.projectList.size() > 0) {

					mProjectItemInfor = (ProjectItemInfo) dc.projectList.get(0);
					AppUtil.setViewText(mTvProjectPayMethod, mProjectItemInfor.repayDivisionWay);
					AppUtil.setViewText(mTvProjectTitle, mProjectItemInfor.loanName);
					demandId = mProjectItemInfor.demandId;
					timeLimit = mProjectItemInfor.timeLimit;
					tradeId = mProjectItemInfor.tradeId;
					interestRate = mProjectItemInfor.interestRate;
					/** 设置借款金额 **/

					if (mProjectInfo == null) {

						String tempBorrowMoney = mProjectItemInfor.loanAmount;
						/** 借款金额 **/
						if (!TextUtils.isEmpty(tempBorrowMoney)) {
							double borrowMoney = 0;
							try {
								borrowMoney = Double.parseDouble(tempBorrowMoney.replace(",", ""));
							} catch (Exception e) {
								e.printStackTrace();
							}
							if (borrowMoney < 10000) {
								mTvProjectBorrow.setText(borrowMoney + "元");
							} else {
								int tenThousand = (int) borrowMoney / 10000;
								double thousand = (borrowMoney % 10000) / 10000d;
								if (thousand > 0) {
									mTvProjectBorrow.setText((tenThousand + thousand) + "万");
								} else {
									mTvProjectBorrow.setText(tenThousand + "万");
								}
							}

						}
					}

					/** 进度条 **/
					if (!TextUtils.isEmpty(mProjectItemInfor.investProgress)) {
						double progress = 0;
						try {
							String strProgress = mProjectItemInfor.investProgress;
							if (strProgress.endsWith("%")) {
								strProgress = strProgress.substring(0, strProgress.length() - 1);
							}
							progress = Double.parseDouble(strProgress);
						} catch (Exception e) {
							e.printStackTrace();
						}
						String temp_progress = "";
						if (!TextUtils.isEmpty(progress + "")) {
							temp_progress = format.format(progress);
							if (temp_progress.indexOf(".") == 0) {
								temp_progress = "0" + temp_progress;
							}
						} else {
							temp_progress = "0.00";
						}
						mPbProjectDetail.setProgress((int) (progress));
						AppUtil.setViewText(mTvProjectProgress, temp_progress + "%");
					}

					AppUtil.setViewText(mTvProjectIncome, mProjectItemInfor.interestRate);
					AppUtil.setViewText(mTvProjectDate, mProjectItemInfor.timeLimit);

					ShapeDrawable drawable = new ShapeDrawable(new RoundRectShape(
							new float[] { 4, 4, 4, 4, 4, 4, 4, 4 }, null, null));
					/*** 借款信息数据 ***/
					loanNote = mProjectItemInfor.loanNote;// 项目信息
					loanStatement = mProjectItemInfor.loanStatement;// 担保机构简介
					guaranteeStatement = mProjectItemInfor.guaranteeStatement;// 风控措施
					projectInfo = mProjectItemInfor.projectInfo;// 涉诉信息
					info3 = mProjectItemInfor.info3;//
					sponsorStatement = mProjectItemInfor.sponsorStatement;// 担保机构意见
					if ("1".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("立即支持");
						mBtnProjectInvest.setClickable(true);
						dismissProgressDialog();

						drawable.getPaint().setColor(Color.parseColor("#03b888"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);

						return;
					}

					if ("2".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("投资已满");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#006f41"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}
					if ("3".equals(mProjectItemInfor.status)) {

						mBtnProjectInvest.setText("未到时间");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#b4b4b4"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}
					if ("4".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("还款完成");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#00819e"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}
					if ("5".equals(mProjectItemInfor.status) || "7".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("正在还款");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#07b9c3"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}
					if ("6".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("已过投资时间");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#00839F"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}
					if ("8".equals(mProjectItemInfor.status)) {
						mBtnProjectInvest.setText("交易失败");
						mBtnProjectInvest.setClickable(false);
						dismissProgressDialog();
						drawable.getPaint().setColor(Color.parseColor("#808080"));
						mBtnProjectInvest.setBackgroundDrawable(drawable);
						return;
					}

				}
			}
			dismissProgressDialog();
		}
	}

	class CheckInvestPasswordTask extends AsyncTask<Void, Void, String> {

		private String mFailstr;
		private String projectId;
		private String password;

		private CheckInvestPasswordClass data = new CheckInvestPasswordClass();

		public CheckInvestPasswordTask(String projectId, String password) {
			this.projectId = projectId;
			this.password = password;

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
		}

		@Override
		protected String doInBackground(Void... params) {

			RequestObject reqObj = new RequestObject();
			reqObj.method = "checkInvestPassword";
			reqObj.map.put("demandId", projectId);
			reqObj.map.put("password", password);
			return requestDataAndGetErrorMsg(reqObj, data);
		}

		@Override
		protected void onPostExecute(String result) {
			if ("-1".equals(data.code)) {
				loginWebview();
				return;
			}
			if (!TextUtils.isEmpty(mFailstr)) {
				showToast(mFailstr);

			} else {
				if ("0".equals(data.code)) {
					showToast(data.msg);
				} else {
					showToast(data.msg);
					// Intent i = new Intent(mContext, MyInvestActivity.class);
					// i.putExtra("tradeId", mProjectItemInfor.tradeId);
					// if (mProjectInfo == null) {
					// i.putExtra("demandId", demandId);
					// i.putExtra("timeLimit", timeLimit);
					// i.putExtra("tradeId", tradeId);
					//
					// } else {
					// i.putExtra("demandId", mProjectInfo.demandId);
					// i.putExtra("timeLimit", mProjectInfo.timeLimit);
					// i.putExtra("tradeId", mProjectInfo.tradeId);
					// }
					// i.putExtra("interestRate", interestRate);
					// startActivityForResult(i,
					// CommonData.REQUEST_CODE_PROJECT);
					dismissProgressDialog();
					// finish();
					getInvestPromise(false);
				}
			}
		}
	}

	/**
	 * 判断是否可以投资
	 */
	public void getInvestPromise(boolean isDirect) {
		showProgressDialog();
		isDirectInvest = isDirect;
		new AcountManageTask(isDirect).execute();
	}

	class AcountManageTask extends AsyncTask<Void, Void, String> {
		private AcountManageDataClass dc = new AcountManageDataClass();

		boolean isDirect;

		public AcountManageTask(boolean isDirect) {
			this.isDirect = isDirect;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			RequestObject mObject = new RequestObject();
			mObject.method = "appAccountInfo";
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				CommonData.FLAG_LOGIN_SERVER = false;
				MyAccountActivity.gIsLoginServer = false;
				CommonData.IS_LOGIN = false;
				Intent intent = new Intent();
				intent.setClass(ProjectDetailActivity.this, LoginActivity.class);
				startActivityForResult(intent, FLAG_INVEST_LOGIN);
				return;
			}
			if (!TextUtils.isEmpty(result)) {
				showToast(result);
			} else {
				AcountManageDataInfo data = dc.accountInfo;		
				
				final String mobile = data.mobile;
				
				String openStatus = getString(R.string.payment_status_on);
				String activeStatus = getString(R.string.payment_status_active);
				if(TextUtils.isEmpty(data.openState) || (!data.openState.equals(openStatus) && !data.openState.equals(activeStatus))) {
					dismissProgressDialog();
					AlertDialog.Builder builder = new AlertDialog.Builder(ProjectDetailActivity.this);
					builder.setTitle("提示");
					builder.setMessage("您的账户未激活，激活支付账户后方可充值、投资。");
					if(!TextUtils.isEmpty(mobile)) {
						builder.setPositiveButton("立即激活", new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(ProjectDetailActivity.this,RegistrationActiveActivity.class);
								intent.putExtra("phoneNum", mobile);
								startActivity(intent);
							}
							
						});
						builder.setNegativeButton("暂不激活", null);
					} else {
						builder.setPositiveButton("确定", null);
					}			
					AlertDialog dialog = builder.create();					
					dialog.show();
					return;
				}

				Intent i = new Intent(mContext, MyInvestActivity.class);
				if (!isDirect) {
					i.putExtra("tradeId", mProjectItemInfor.tradeId);
				}
				if (mProjectInfo == null) {
					i.putExtra("demandId", demandId);
					i.putExtra("timeLimit", timeLimit);
					i.putExtra("tradeId", tradeId);

				} else {
					i.putExtra("demandId", mProjectInfo.demandId);
					i.putExtra("timeLimit", mProjectInfo.timeLimit);
					i.putExtra("tradeId", mProjectInfo.tradeId);
				}
				i.putExtra("interestRate", interestRate);
				if (isDirect) {
					i.putExtra("tradeId", mProjectItemInfor.tradeId);
				}
				startActivityForResult(i, CommonData.REQUEST_CODE_PROJECT);
			}

		}
	}

}
